# -*- coding: utf-8 -*-
from odoo import models, api, fields, tools
from odoo.exceptions import UserError
import re


class crop_promotion(models.Model):

    _name = "datcent.crop_promotion"
    _description = "品种推广"
    _table = "datcent_crop_promotion"
    _order = "sequence"
    _inherit =  ['mail.thread', 'mail.activity.mixin', 'image.mixin']

    sequence = fields.Integer(string="排序",translate = 0)
    name = fields.Many2one(string="品种名称",comodel_name = 'datcent.crop_varieties',translate = 0)
    crop_id = fields.Many2one(string="作物名称",comodel_name = 'datcent.crop',related = 'name.crop_id',translate = 0)
    crop_category_id = fields.Many2one(string="作物类别",comodel_name = 'datcent.crop_category',related = 'name.crop_category_id',translate = 0)
    family_genus_id = fields.Many2one(string="科属种",comodel_name = 'datcent.family_genus',related = 'name.family_genus_id',translate = 0)
    file_no = fields.Char(string="审定/登记编号",related = 'name.file_no',translate = 0)
    authorize_type = fields.Selection(string="审定/登记类型",selection = [('1', '国审'), ('2', '省审')],default='1',translate = 0)
    is_PO_license = fields.Boolean(string="是否有生产经营许可证",related = 'name.is_PO_license',translate = 0)
    is_variety_right = fields.Boolean(string="是否有品种权",related = 'name.is_variety_right',translate = 0)
    is_promote_variety = fields.Boolean(string="是否有品种推广权",related = 'name.is_promote_variety',translate = 0)
    registration_date = fields.Date(string="审定/登记年份",related = 'name.product_date',translate = 0)
    author = fields.Char(string="申请者",related = 'name.author',translate = 0)
    country_id = fields.Many2one(string="国家",comodel_name = 'res.country',related = 'name.country_id',default = lambda self:self.env['res.country'].search([('code','=','CN')]),translate = 0)
    promotion_items = fields.One2many(string="推广情况",comodel_name = 'datcent.crop_promotion_item',inverse_name = 'main_id',translate = 0)
    memo = fields.Text(string="简要说明",translate = 0)
    url = fields.Char(string="相关链接",translate = 0)
    image = fields.Binary(string="推广照片1",translate = 0)
    image1 = fields.Binary(string="推广照片2",translate = 0)
    detail_contents = fields.Html(string="详细介绍",translate = 0)
    attachment = fields.Many2many(string="相关附件",comodel_name = 'ir.attachment',translate = 0)
    state = fields.Selection(string="状态",selection = [('1', '草稿'), ('2', '提交'), ('3', '审核')],default='1',translate = 0)

    def button1(self):
        if self.state == '1':
            self.write({'state': '2'})
        # 审核
    def button2(self):
        if self.state == '2':
            self.write({'state': '3'})
        # 退回
    def button3(self):
        if self.state == '2':
            self.write({'state': '1'})

        # 反审核
    def button4(self):
        if self.state == '3':
            self.write({'state': '2'})
